package graphics.screens;

import java.io.IOException;

import tool.Arrays;
import boot.Boot;
import db.ArtistRecord;
import db.Mp3DBRecord;
import db.SimpleDBListener;
import db.SimpleDBReader;

public class ArtistsDBListener implements SimpleDBListener {

	private String lcSearchEntry;
	private SimpleDBReader dbReader;
	private ArtistRecord[] artistRecords;
	private int index = 0;
	private boolean ignoreRecords =  true;
	private Mp3DBRecord currentMP3Record;

	public ArtistsDBListener(String aSearchEntry, SimpleDBReader aDBReader) {
		lcSearchEntry = aSearchEntry.toLowerCase();
		dbReader = aDBReader;
		artistRecords = new ArtistRecord[100];
		Arrays.initialize(artistRecords);
	}
	
	public void processLine(String line) {
		if (isArtist(line)) {
			
			String lcArtist = getKey(line);
			if (lcArtist.compareTo(lcSearchEntry) > 0) {
//				Boot.debugAlert("Finalized on\n" + line);
				finalize();
			} else if (lcArtist.equals(lcSearchEntry)) {
				if (artistRecords[index] != null) {					
					index++;
				}
				artistRecords[index] = new ArtistRecord(line);
				ignoreRecords  = false;
			}
		} else if (!ignoreRecords) {
			addDetail(line);
		}		
	}

	private String getKey(String line) {
		String result = null;
		int length = lcSearchEntry.length();
		if (line.length() >= length) {
			result = line.toLowerCase().substring(0, length);
		} else {
			int diff = length - line.length();
			result = line;
			for (int i = 0; i < diff; i++) {
				result += " ";
			}
		}
		return result;
	}

	private void addDetail(String line) {
		String neatLine = removeTabs(line);
		if (isTitle(line)) {
			currentMP3Record = new Mp3DBRecord(neatLine);
		} else if (isRating(line)) {
			currentMP3Record.setRating(neatLine);
			artistRecords[index].addMP3Record(currentMP3Record);
		} else if (isRelativeFolder(line)) {
			currentMP3Record.setFolder(neatLine);
		}
	}

	private String removeTabs(String line) {
		String result = line;
		
		while (result.startsWith("\t")) {
			result = result.substring(1);
		}
		return result;
	}

	private void finalize() {
		try {
			dbReader.stopReading();
		} catch (IOException e) {
			Boot.alertException(e, this, "finalize()");
		}
	}

	private void trimArtists() {
		int i = 0;
		
		while ((artistRecords[i] != null) && (i < artistRecords.length))  {
			i++;
		}
		ArtistRecord[] temp = new ArtistRecord[i];
		
		for (int j = 0; j < temp.length; j++) {
			temp[j] = artistRecords[j];
		}
		artistRecords = temp;
//		String list = "Artists:\n";
//		
//		for (int j = 0; j < artistRecords.length; j++) {
//			list += artistRecords[j].getArtistName() + "\n";
//		}
//		Boot.debugAlert(list);
	}

	private boolean isRelativeFolder(String line) {
		return line.startsWith("\t\t") && (!isRating(line));
	}

	private boolean isRating(String line) {
		return ("\t\tGold".equals(line)) || ("\t\tBasic".equals(line));
	}

	private boolean isTitle(String line) {
		return (line.startsWith("\t")) && (!line.startsWith("\t\t"));
	}

	private boolean isArtist(String line) {
		return !line.startsWith("\t");
	}

	public ArtistRecord[] getArtistRecords() {
		return artistRecords;
	}

	public void readingDone() {
		trimArtists();
	}

}
